//
//  QuickCircleTapScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/9/13.
//
//

#include "QuickCircleTapScene.h"
#include "SceneManager.h"
#include "GameLayerManager.h"
#include "MyUtilities.h"
CCScene* QuickCircleTapScene::scene(int numberOfSeconds){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    QuickCircleTapScene *layer = QuickCircleTapScene::create(numberOfSeconds);
    GameLayerManager::sharedInstance()->quickCircleTapLayer = layer;
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}


bool QuickCircleTapScene::init(){
    
    CountDownGameScene::init();
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonWidth = winSize.width * 0.3f;
    double radius = (winSize.width- buttonWidth)/2;
    CCPoint originPoint = ccp((winSize.width - buttonWidth)/2, ((winSize.height - scoreAndTimeLayer->getBarHeight()-buttonWidth)/2));
    button = MyRotatingButton::create("circle_button.png", (winSize.width - buttonWidth)/2, ((winSize.height - scoreAndTimeLayer->getBarHeight()-buttonWidth)/2 + radius), buttonWidth, buttonWidth);
    button->setCircle(originPoint, radius);
    CCCallFuncO* callBackOnTap = CCCallFuncO::create(this, callfuncO_selector(StaticTapScene::onTapTheButton), this);
    button->addCallback(callBackOnTap);
    this->addChild(button,1);
    button->randomDirection();
    
    return true;
}

QuickCircleTapScene* QuickCircleTapScene::create(int numberOfSeconds){
    QuickCircleTapScene* scene = QuickCircleTapScene::create();
    scene->maxTimeCountDown = numberOfSeconds;
    scene->timeRemaining = scene->maxTimeCountDown;
    scene->scoreAndTimeLayer->updateTime(scene->timeRemaining);
    return scene;
}

#pragma mark - startGame
//SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->staticTapScene;

void QuickCircleTapScene::update(float dt){
    button->handleUpdate(dt);
}

void QuickCircleTapScene::onTapTheButton(cocos2d::CCObject *sender){
    
    numberOfTap++;
    scoreAndTimeLayer->updateScore(numberOfTap);
}

void QuickCircleTapScene::startGame(){
    CountDownGameScene::startGame();
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->quickCircleTapScene;
    this->scheduleUpdate();

}

void QuickCircleTapScene::onTick(float dt){
    CountDownGameScene::onTick(dt);
    if(button->isAtMaxSpeed()==false){
        button->increseVelocityBy(M_PI/(360));
    }
}



void QuickCircleTapScene::reset(){
    CountDownGameScene::reset();
    button->resetState();
    button->randomDirection();

}

#pragma mark - event handling


void QuickCircleTapScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    button->handleTouchesBegan(touches, pEvent);
}

void QuickCircleTapScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    button->handleTouchesMoved(touches, pEvent);
}

void QuickCircleTapScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    button->handleTouchesEnded(touches, pEvent);
}

